package com.ydzt.fanka.server.timer;

import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;

import com.ydzt.fanka.server.init.InitData;
import com.ydzt.fanka.server.utils.DBHelper;
import com.ydzt.fanka.server.utils.NetUtil;
import com.ydzt.fanka.server.utils.PackageHelper;

/*
 * 食堂日终定时器
 * 食堂定时向中心发送食堂的结算日期 
 * 中心会对结算日期做出判断 控制食堂是否进行日终操作
 * */
public class DREndDayTimer {
	static Logger logger = Logger.getLogger(DREndDayTimer.class.getName());
	static String strCurrentStmtDay = "";
	static byte[] byteArrayCurrentStmtDay;
	static byte[] typeBytes;
	static byte[] pkg_2002;
	static byte[] sender;
	static byte[] recevier;
	static byte[] port;
	static byte[] msgMgrType;
	static byte[] seq;
	static byte[] pkgno;
	static byte[] lastFlagBytes;
	static byte[] recordcount;
	static byte[] agm;
	
	public static void autoEndDay(){
		Timer timer = new Timer();
		//进程启动5s后 开始执行GCTask 周期是30000毫秒
		timer.schedule(new GCTask(), 5000, Long.parseLong(InitData.PERIOD_DR_ENDDAY));
	}
	
    static class GCTask extends TimerTask{
    	@Override
    	public void run() {
    		strCurrentStmtDay = DBHelper.getStmtDay();
    		if(strCurrentStmtDay.length() == 8){
    			byteArrayCurrentStmtDay = new byte[4];
        		//组2001报文给中心
        		pkg_2002 = new byte[27];
        		pkg_2002 = PackageHelper.createPkg2002(strCurrentStmtDay, "00000000");
    			//发送2002报文
    			try {
    				logger.info("发送2002 进行结算日期验证");
    				if(0 == NetUtil.synSendMsg(pkg_2002)){
						logger.info("发送2002 进行结算日期验证 成功!");
					}
					else{
						logger.error("发送2002 进行结算日期验证 失败!");
					}
    			} catch (Exception e) {
    				logger.error("发送2002报文出错!" + e.getMessage());
    			}
    		}
    	}
	}
}
